package com.wufun.overseas.adapters;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.wufun.baselibrary.download.CommonDownloadTask;
import com.wufun.baselibrary.download.UtilsMy;
import com.wufun.baselibrary.impl.AdapterInterface;
import com.wufun.overseas.R;
import com.wufun.overseas.main.activity.GameDetailActivity;
import com.wufun.overseas.main.activity.PostDetailsActivtiy;
import com.wufun.overseas.main.fragment.HomeFragment;
import com.wufun.overseas.models.bean.MyHomeBean;
import com.wufun.overseas.models.db.DownLoadModel;
import com.wufun.overseas.tools.DownloadManager;
import com.wufun.overseas.utils.AppDevicesUtil;
import com.wufun.overseas.utils.PostNetDotUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yangg on 2017/12/2.
 */

public class MyHomerAdapter extends BaseAdapter  {

    int state;
    String btn_state;
    boolean finsh;
    Context mContext;
    HomeFragment homeFragment;
    //    MyHomeBean mylists;
    List<MyHomeBean.DataBean.ListBean> mylist = new ArrayList<>();
    private AdapterInterface mAdapterInterface;
    int pro;
    String speed;

    private boolean isReflash = false;

    public void setReflash() {
        isReflash = !isReflash;
        notifyDataSetChanged();
    }

    public MyHomerAdapter(Context context, AdapterInterface mAdapterInterface) {
        this.mContext = context;
        this.mAdapterInterface = mAdapterInterface;
    }

    public void  getSpeed(int pros,String speeds){
        pro=pros;
        speed=speeds;
    }


    public List<MyHomeBean.DataBean.ListBean> getData() {
        return mylist;
    }

    @Override
    public int getCount() {
        return mylist.size();
    }

    @Override
    public MyHomeBean.DataBean.ListBean getItem(int position) {
        return mylist.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getViewTypeCount() {

        return 3;
    }

    @Override
    public int getItemViewType(int position) {
        if (mylist.get(position).getDisplay_type() == 1) {
            return 0;
        } else if (mylist.get(position).getDisplay_type() == 2) {
            return 1;
        } else {
            return 2;
        }

    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        ViewHolder viewHolder = null;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.home_addpter, null);
            viewHolder.tv_titleRom = (TextView) convertView.findViewById(R.id.titleRom);
            viewHolder.tv_titleSecondRom = (TextView) convertView.findViewById(R.id.titleSecondRom);
            viewHolder.tv_GameName_home = (TextView) convertView.findViewById(R.id.GameName_home);
            viewHolder.tv_gamesize = (TextView) convertView.findViewById(R.id.gamesize);
            viewHolder.tv_address = (TextView) convertView.findViewById(R.id.address);
            viewHolder.tv_openSize = (TextView) convertView.findViewById(R.id.openSize);
            viewHolder.tv_up = (TextView) convertView.findViewById(R.id.up_like);
            viewHolder.tv_revert = (TextView) convertView.findViewById(R.id.revert);
            viewHolder.iv_img_home = (SimpleDraweeView) convertView.findViewById(R.id.img_home);
            viewHolder.iv_likesize = (ImageView) convertView.findViewById(R.id.likesize);
            viewHolder.iv_openreverrt = (ImageView) convertView.findViewById(R.id.openrevert);
            viewHolder.btn_download_ll = (LinearLayout) convertView.findViewById(R.id.btn_download_ll);
            viewHolder.layout = (LinearLayout) convertView.findViewById(R.id.layout_game);
            viewHolder.layout_game_community = (LinearLayout) convertView.findViewById(R.id.layout_game_community);
            viewHolder.tv_tag = (TextView) convertView.findViewById(R.id.tv_tag);
            viewHolder.ll_all = (LinearLayout) convertView.findViewById(R.id.ll_all);
            viewHolder.btn_download = (TextView) convertView.findViewById(R.id.btn_download);
            viewHolder.tv_desc = (TextView) convertView.findViewById(R.id.tv_desc);
            viewHolder.ll_desc_size = (LinearLayout) convertView.findViewById(R.id.ll_desc_size);  //未带progressbar
            viewHolder.ll_ststus = (LinearLayout) convertView.findViewById(R.id.ll_ststus);
            viewHolder.ll_progress_bar = (LinearLayout) convertView.findViewById(R.id.ll_progress);    //带有progressabr
            viewHolder.tv_loadCount = (TextView) convertView.findViewById(R.id.loadCount);             //下载
            viewHolder.tv_state = (TextView) convertView.findViewById(R.id.tv_state);                 //下载网速
            viewHolder.progressBar = (ProgressBar) convertView.findViewById(R.id.progressBar);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.tv_titleRom.setText(mylist.get(position).getSubject());
        viewHolder.tv_titleSecondRom.setText(mylist.get(position).getMessage());
        viewHolder.tv_address.setText(mylist.get(position).getNickname());
        if (mylist.get(position).getTags() != null
                && mylist.get(position).getTags().size() > 0) {
            viewHolder.tv_tag.setText(mylist.get(position).getTags().get(0).getTag_name());
        }

        viewHolder.tv_up.setText(mylist.get(position).getPraise() + "");
        viewHolder.tv_revert.setText(mylist.get(position).getCommit() + "");
        viewHolder.tv_openSize.setText(mylist.get(position).getView() + "");  //不知道是哪个

        switch (getItemViewType(position)) {
            case 0:
                // google play
                if (mylist.get(position).getGoole_play() != null) {
                    viewHolder.iv_img_home.setImageURI(mylist.get(position).getGoole_play().getIcon());
                    viewHolder.tv_gamesize.setText(mylist.get(position).getGoole_play().getQuality());
                    viewHolder.tv_gamesize.setVisibility(View.VISIBLE);
                    viewHolder.btn_download.setVisibility(View.VISIBLE);
                    viewHolder.btn_download.setText("Google play");
                    viewHolder.btn_download_ll.setBackgroundResource(R.drawable.recom_blue_butn);
                    viewHolder.btn_download.setTextColor(mContext.getResources().getColor(R.color.app_blue_color));
                    viewHolder.iv_img_home.setImageURI(mylist.get(position).getGoole_play().getIcon());
                    viewHolder.tv_GameName_home.setText(mylist.get(position).getGoole_play().getGame_name());
                    viewHolder.tv_desc.setText(mylist.get(position).getGoole_play().getDescription());
                    viewHolder.tv_desc.setVisibility(View.VISIBLE);
                }
                break;
            case 1:
                //视频
                viewHolder.iv_img_home.setImageURI(mylist.get(position).getRelation_game().getGame_ico_remote());
                viewHolder.tv_gamesize.setVisibility(View.GONE);
                viewHolder.btn_download_ll.setVisibility(View.GONE);
                viewHolder.btn_download.setText("Download");
                viewHolder.iv_img_home.setImageResource(R.mipmap.youtobe_icon);
                viewHolder.tv_GameName_home.setText(mylist.get(position).getNickname());
                viewHolder.tv_desc.setVisibility(View.GONE);
                break;
            case 2:
                //待下载
                viewHolder.iv_img_home.setImageURI(mylist.get(position).getRelation_game().getGame_ico_remote());
                viewHolder.tv_gamesize.setText(mylist.get(position).getRelation_game().getGame_size() + "M");
                viewHolder.tv_gamesize.setVisibility(View.VISIBLE);
                viewHolder.btn_download_ll.setVisibility(View.VISIBLE);
                viewHolder.tv_GameName_home.setText(mylist.get(position).getRelation_game().getGame_name());
                viewHolder.tv_desc.setText(mylist.get(position).getRelation_game().getGame_desc());
                viewHolder.tv_desc.setVisibility(View.VISIBLE);
                state = mylist.get(position).getGame_state();
                try {
                    viewHolder.btn_download_ll.setBackgroundResource(R.drawable.recom_blue_butn);
                    viewHolder.btn_download.setTextColor(mContext.getResources().getColor(R.color.app_blue_color));
                    //目前有的下载地址为空， 且uri后缀为.apk
                    String down_uri = mylist.get(position).getRelation_game().getGame_down_url_mote();

                    if (down_uri != null && down_uri.indexOf("apk") == -1) {
                        state = DownloadManager.getInstance().getDownloadTaskStatus(mylist.get(position).getRelation_game().getGame_down_url_mote());
                        switch (state) {
                            case CommonDownloadTask.STATUS_NORMAL:  //未下载
                            case CommonDownloadTask.STATUS_NOT:
                                viewHolder.ll_progress_bar.setVisibility(View.GONE);
                                viewHolder.ll_desc_size.setVisibility(View.VISIBLE);
                                btn_state = mContext.getResources().getString(R.string.download_status_start_str);

                                break;
                            case CommonDownloadTask.STATUS_START:   //下载中
                            case CommonDownloadTask.STATUS_PROGRESS: //下载中
                                viewHolder.ll_progress_bar.setVisibility(View.VISIBLE);
                                viewHolder.ll_desc_size.setVisibility(View.GONE);
                                btn_state = mContext.getResources().getString(R.string.download_stop_str);
                                String str = mylist.get(position).getRelation_game().getGame_size();
                                double size=  Double.valueOf(mylist.get(position).getRelation_game().getGame_size());
                                size = size * 1024 * 1024;
                                double count=pro*size*100/10000;
                                long countsize= (long) count;
                                viewHolder.tv_loadCount.setText(UtilsMy.FormatFileSize(countsize) + "/"+UtilsMy.FormatFileSize((long) (size)));
                                viewHolder.tv_state.setText(speed==null?"0KB":speed+"/s");
                                break;
                            case CommonDownloadTask.STATUS_FINISH:   //下载完成
                                btn_state = mContext.getResources().getString(R.string.download_status_open_str);
                                viewHolder.ll_progress_bar.setVisibility(View.GONE);
                                viewHolder.ll_desc_size.setVisibility(View.VISIBLE);
                                break;
                            case CommonDownloadTask.STATUS_STOP:   //暂停下载
                                viewHolder.ll_progress_bar.setVisibility(View.VISIBLE);
                                viewHolder.ll_desc_size.setVisibility(View.GONE);
                                btn_state = mContext.getResources().getString(R.string.download_status_goon_str);
                                viewHolder.tv_state.setText(mContext.getResources().getString(R.string.download_stop_str));
                                break;
                            case CommonDownloadTask.STATUS_ERROR:   //下载错误
                                btn_state = mContext.getResources().getString(R.string.download_fail_str);
                                viewHolder.btn_download_ll.setBackgroundResource(R.drawable.recom_orange_butn);
                                viewHolder.btn_download.setTextColor(mContext.getResources().getColor(R.color.app_main_color));
                                viewHolder.ll_progress_bar.setVisibility(View.VISIBLE);
                                viewHolder.ll_desc_size.setVisibility(View.GONE);
                                break;
                            case CommonDownloadTask.STATUS_ZIP_PROGRESS: //解压中
                            case CommonDownloadTask.STATUS_STSRT_ZIP:  //开始解压
                                btn_state = mContext.getResources().getString(R.string.zip_str);
                                viewHolder.ll_progress_bar.setVisibility(View.GONE);
                                viewHolder.ll_desc_size.setVisibility(View.VISIBLE);
                                break;
                            case CommonDownloadTask.STATUS_ZIP_FAIL:
                                btn_state = mContext.getResources().getString(R.string.zip_fail_str);
                                viewHolder.btn_download_ll.setBackgroundResource(R.drawable.recom_orange_butn);
                                viewHolder.btn_download.setTextColor(mContext.getResources().getColor(R.color.app_main_color));
                                break;

                        }
                        Log.e("TAG", "状态码：" + state);
                        viewHolder.btn_download.setText(btn_state);
                    } else {
                        btn_state = "Download";
                        Log.e("TAG", "URI ERROR");
                    }
                } catch (Exception e) {
                    btn_state = "Download";
                    e.printStackTrace();
                    break;
                }
                Log.e("TAG", "状态码：" + state);
                viewHolder.btn_download.setText(btn_state);
                break;

            default:
                //目前显示的类型 只有 视频  带下载的连接 和google play。
                viewHolder.ll_all.setVisibility(View.GONE);
                break;
        }

        if (viewHolder.progressBar.getVisibility() == View.VISIBLE) {
            DownLoadModel temp = DownloadManager.getInstance().getDownloadByUrl(mylist.get(position).getRelation_game().getGame_down_url_mote());
            if (temp!=null)
                viewHolder.progressBar.setProgress(temp.getProgress());
        }

        //标题 跳转帖子详情
        viewHolder.tv_titleRom.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mylist.get(position).getDisplay_type() == 1) {
                    // google play


                } else if (mylist.get(position).getDisplay_type() == 2) {
                    startGoActivity(mylist.get(position).getPid(), mylist.get(position).getSubject());
                } else if (mylist.get(position).getDisplay_type() == 3) {
                    // 待下载 跳转帖子详情
                    startGoActivity(mylist.get(position).getPid(), mylist.get(position).getRelation_game().getGame_name());
                }


            }
        });
        //二级标题 a=1 跳帖子详情 2跳视频播放页面
        viewHolder.tv_titleSecondRom.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mylist.get(position).getDisplay_type() == 1) {
                    // google play

                } else if (mylist.get(position).getDisplay_type() == 2) {
                    startGoActivity(mylist.get(position).getPid(), mylist.get(position).getSubject());
                } else if (mylist.get(position).getDisplay_type() == 3) {
                    // 待下载 跳转帖子详情
                    startGoActivity(mylist.get(position).getPid(), mylist.get(position).getRelation_game().getGame_name());
                }
            }
        });

        viewHolder.layout_game_community.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mylist.get(position).getDisplay_type() == 1) {
                    // google play

                } else if (mylist.get(position).getDisplay_type() == 2) {
                    //内层点击  接着跳详情
                    startGoActivity(mylist.get(position).getPid(), mylist.get(position).getSubject());
                } else if (mylist.get(position).getDisplay_type() == 3) {
                    // 内层 跳游戏列表
                    startGoGameDe(mylist.get(position).getFid(), mylist.get(position).getRelation_game().getGame_name(), mylist.get(position).getRelation_game().getGame_ico_remote());
                }
            }
        });


        viewHolder.ll_ststus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 取消点击效果

            }
        });
        //  下载按钮
        final ViewHolder finalViewHolder = viewHolder;
        viewHolder.btn_download_ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                  mylist.get(position).getRelation_game().getGame_down_url_mote();   下载游戏的 url
                if (mylist.get(position).getDisplay_type() == 1) {
                    //google paly package_name
                    String google_package = "com.android.vending";
                    if (mylist.get(position).getGoole_play().getPackage_name() == null) {
                        return;
                    }
                    AppDevicesUtil.launchAppDetail(mylist.get(position).getGoole_play().getPackage_name(), google_package, mContext);
                    PostNetDotUtil.PostNetDot(mContext, mylist.get(position).getGoole_play().getPackage_name(), mylist.get(position).getPid());         //打点
                } else if (mylist.get(position).getDisplay_type() == 3) {
                    if (state == 1 || state == -2 || state == 0 || state == 4||CommonDownloadTask.STATUS_STOP==state
                            ||CommonDownloadTask.STATUS_PROGRESS==state||CommonDownloadTask.STATUS_START==state) {
                        mAdapterInterface.onAdapterItemClick(v, position);
                    }
                }

            }
        });

        return convertView;
    }


    public void startGoActivity(String tid, String title) {
        //帖子详情 标题，二级标题   帖子id  还有区分 搜索的type
        Intent intent = new Intent(mContext, PostDetailsActivtiy.class);
        intent.putExtra("title", title);
        intent.putExtra("Tid", tid);
        intent.putExtra("type", 1);
        mContext.startActivity(intent);
    }


    public void startGoGameDe(String fid, String gameName, String imguri) {
        //内层点击
        Intent intent = new Intent(mContext, GameDetailActivity.class);
        intent.putExtra("title", gameName);
        intent.putExtra("imguri", imguri);
        intent.putExtra("fid", fid);
        mContext.startActivity(intent);
    }

    class ViewHolder {
        TextView tv_titleRom, tv_titleSecondRom, tv_GameName_home, tv_gamesize,
                tv_address, tv_openSize, tv_up, tv_revert, tv_tag, tv_desc;
        ImageView iv_likesize, iv_openreverrt;
        SimpleDraweeView iv_img_home;
        LinearLayout btn_download_ll;
        LinearLayout layout, ll_all;
        TextView btn_download, tv_loadCount, tv_state;
        LinearLayout layout_game_community, ll_progress_bar, ll_desc_size, ll_ststus;
        ProgressBar progressBar;

    }
}
